package com.nguc.ngucpractice.controller;

import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.nguc.ngucpractice.common.controller.BaseController;
import com.nguc.ngucpractice.common.entity.Result;
import com.nguc.ngucpractice.utils.OSSUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * User:曹帅
 * Date:2020/8/7
 * Version:1.0
 */
@RestController
@RequestMapping("/manage/img")
public class OssController extends BaseController {

    @Value("${spring.access-key}")
    private String accessKey;

    @Value("${spring.secret-key}")
    private String secretKey;

    @Value("${spring.oss.endpoint}")
    private String endPoint;

    @Value("${spring.oss.bucket}")
    private String bucket;

    @Value("${spring.oss.arn}")
    private String arn;


    @RequestMapping(value = "/token", method = RequestMethod.GET)
    public ModelAndView findAll(@RequestParam(name = "userId", required = true) String userId) {
        Result result = new Result();
        try {
            AssumeRoleResponse response = OSSUtils.buildAliyunSTSCredentials(accessKey, secretKey, endPoint, arn, userId);
            result.setStatus("0");
            result.setData(response);
        } catch (ClientException e) {
            result.setStatus("1");
            result.setData("获取token失败");
            e.printStackTrace();
        }
        return feedback(result);
    }
}
